<?php
class MigrationPage extends BaseAdminPage {

	function MigrationPage() {
		parent::BaseAdminPage('MigrationPage');
	}

	function enterPage() {
		parent::enterPage();
	}

	function leavePage() {
		parent::leavePage();
	}

	public function serveRequest() {
		parent::serveRequest();

		if (UserHandler::isRoot()) {
			$tpl = BaseAdminPage::loadTemplateFile("devtools/migration.tpl.html");
			TextHandler::insertTextsIntoTemplate($tpl);
			$tpl->setVariable('app_root_url', APP_ROOT_URL);
	
			$tpl->setVariable('link_migratearticles', HydraLinkProvider::getLink('rop_migratearticles'));
			$tpl->setVariable('link_migratetournaments', HydraLinkProvider::getLink('rop_migratetournaments'));

			$action = FunctionLibrary::getGetValue('action');
			if ($action) {
				$action = 'action_'.$action;
				$this->$action();
			}
	
			return $tpl->get();
		} else {
			return TextHandler::getText("migr_err_nopermission");
		}
	}

	public function action_migrateArticles() {
		try {
			MigrationHandler::migrateArticles();
			handleSuccess('Articles and menus successfully migrated.', HydraLinkProvider::getLink('rop_migration'));
			exit();
		} catch (Exception $e) {
			handleError($e->getMessage(), HydraLinkProvider::getLink('rop_migration'));
			exit();
		}
	}

	public function action_migrateTournaments() {
		try {
			MigrationHandler::migrateTournaments();
			handleSuccess('Tournaments successfully migrated.', HydraLinkProvider::getLink('rop_migration'));
			exit();
		} catch (Exception $e) {
			handleError($e->getMessage(), HydraLinkProvider::getLink('rop_migration'));
			exit();
		}
	}

}
?>